package fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yztc.thinkpad.mandemo.R;
import com.yztc.thinkpad.mandemo.single.FragmentAdapter;
import com.yztc.thinkpad.mandemo.single.SingleViewpagerFragment;
import com.yztc.thinkpad.mandemo.single.base.BaseFragment;
import com.yztc.thinkpad.mandemo.single.bean.ContentBean;
import com.yztc.thinkpad.mandemo.single.bean.DataBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import utils.Okhttputils;

/**
 * Created by Administrator on 2016/9/29.
 */
public class FragmentSingle extends BaseFragment {

    @Bind(R.id.toolbar_single)
    Toolbar mToolbarSingle;
    @Bind(R.id.single_tablayout)
    TabLayout mSingleTablayout;
    @Bind(R.id.single_vp)
    ViewPager mSingleVp;
    private List<SingleViewpagerFragment> mList = new ArrayList<>();
    private List<DataBean> totallist = new ArrayList<>();
    private List<ContentBean.DataBean> contentlist = new ArrayList<>();
    private FragmentAdapter mAdapter;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    totallist = (List<DataBean>) msg.obj;
                    for (int i = 0; i <totallist.size() ; i++) {
                        SingleViewpagerFragment fragment = new SingleViewpagerFragment();
                        fragment.setDataBean(totallist.get(i));
                        mList.add(fragment);
                    }
                    mAdapter = new FragmentAdapter(getActivity().getSupportFragmentManager(),mList);
                    mSingleVp.setAdapter(mAdapter);
                    mSingleVp.setOffscreenPageLimit(5);
                    mSingleTablayout.setupWithViewPager(mSingleVp);
                    break;
            }
        }
    };
    @Override
    protected void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String typeurl = "http://api.nanyibang.com/select-condition?administrativeArea=%E5%8C%97%E4%BA%AC%E5%B8%82&age=24&channel=yingyongbao&country=%E4%B8%AD%E5%9B%BD&locality=%E5%8C%97%E4%BA%AC%E5%B8%82&system_name=android&versionCode=219";
                    String typejson = Okhttputils.getInstance().run(typeurl);
                    String typedata = JSONObject.parseObject(typejson).getString("data");
                    List<DataBean> typrlist = new ArrayList<>();
                    typrlist = JSONArray.parseArray(typedata,DataBean.class);
                    Message message = new Message();
                    message.what = 0;
                    message.obj = typrlist;
                    handler.sendMessage(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }

    @Override
    protected void initView() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_single;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
